package com.pl.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.pl.entity.Student;
import com.pl.entity.SysUser;
import com.pl.entity.WmWarehouse;
import com.pl.dao.WmWarehouseDao;
import com.pl.lister.StudentReadLister;
import com.pl.lister.WmWarehouseReadLister;
import com.pl.service.WmWarehouseService;
import org.apache.catalina.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 仓库表(WmWarehouse)表服务实现类
 *
 * @author makejava
 * @since 2024-04-26 18:46:03
 */
@Service("wmWarehouseService")
public class WmWarehouseServiceImpl implements WmWarehouseService {
    @Autowired
    private WmWarehouseDao wmWarehouseDao;

    /**
     * 通过ID查询单条数据
     *
     * @param warehouseId 主键
     * @return 实例对象
     */
    @Override
    public WmWarehouse queryById(Long warehouseId) {
        return this.wmWarehouseDao.queryById(warehouseId);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public List<WmWarehouse> queryByPage(Map<String, Object> map) {
        return wmWarehouseDao.queryAllByLimit(map);
    }

    /**
     * 新增数据
     *
     * @param wmWarehouse 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(WmWarehouse wmWarehouse,HttpSession session) {
        SysUser user = (SysUser) session.getAttribute("user");
        wmWarehouse.setCreateBy(user.getUserName());
        wmWarehouse.setCreateTime(new Date());
        wmWarehouse.setUpdateTime(new Date());
        int insert = this.wmWarehouseDao.insert(wmWarehouse);
        if (insert > 0) {
            return true;
        }
        return false;
    }

    /**
     * 修改数据
     *
     * @param wmWarehouse 实例对象
     * @return 实例对象
     */
    @Override
    public boolean update(WmWarehouse wmWarehouse) {
        wmWarehouse.setCreateTime(new Date());
        int update = this.wmWarehouseDao.update(wmWarehouse);
        if (update>0){
            return true;
        }
        return false;
    }

    /**
     * 通过主键删除数据
     *
     * @param warehouseId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long warehouseId) {
        return this.wmWarehouseDao.deleteById(warehouseId) > 0;
    }

    /**
     * 批量删除
     * @param list
     * @return
     */
    @Override
    public boolean deleteBatch(List<Integer> list) {
       int num= wmWarehouseDao.deleBatch(list);
        if (num>0){
            return true;
        }

        return false;
    }

    /**
     * 批量添加
     * @param list
     * @return
     */
    @Override
    public int insertBatch( List<WmWarehouse> list) {
        return wmWarehouseDao.insertBatch(list);
    }

    /**
     * 导入文件
     * @param file
     * @return
     */
    @Override
    public String importwmWarehouseFile(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), WmWarehouse.class, new WmWarehouseReadLister(this))
                    .sheet("仓库信息").doReadSync();
            return "import_success";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "fail";
    }

    /**
     * 查找全部数据
     * @return
     */
    @Override
    public boolean finAll() {
        List<WmWarehouse> wmWarehouses = wmWarehouseDao.finAll();
        if (wmWarehouses!=null){
           return true;
       }
        return false;
    }

    /**
     * 导出文件
     * @param response
     */
    @Override
    public void exportwmWarehouseFile(HttpServletResponse response) {
        List<WmWarehouse> wmWarehouses= wmWarehouseDao.finAll();
        try {
            EasyExcel.write(response.getOutputStream(), WmWarehouse.class)
                    .sheet("仓库信息")
                    .doWrite(wmWarehouses);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
